
package myprogramui.comp;

import controller.MyController;
import helptest.DiCengOper;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.Stage;
import javax.swing.JFrame;
import myprogramui.comp.filechannel.MyFileChannel;
import myproj.Main;

/**
 *
 * @author Administrator
 */
public class Header extends Group{
    private Stage primaryStage;
    private MyController controller;
    private ImageView logoIV;
    private ImageView toolbox;
    private ImageView setting;
    private ImageView min;
    private ImageView max;
    private ImageView exit;
    private DiCengOper dco;
    //设定各种tooltip
    Tooltip logoTip=new Tooltip("煤矿注浆三维模拟评价系统\n基于java8.0");
    Tooltip toolTip=new Tooltip("工具箱");
    Tooltip setTip=new Tooltip("设置");
    Tooltip minTip=new Tooltip("最小化");
    Tooltip maxTip=new Tooltip("去3D界面");
    Tooltip exitTip=new Tooltip("离开");
    //这里是控制这个到底要不要再绘制数数据了
    private int HUIZHICOUNT = 0;
    //这里是数据恢复按钮点按的次数
    private int SHUJUHUIFUI=0;
    public Header(final Stage primaryStage,final Main threeDMain, final MyController controller){
        this.primaryStage=primaryStage;
        this.controller=controller;
        //这是logo，文字，上边栏
        logoIV=new ImageView("Image_no_pair/title.png");
        logoIV.setX(240);
        logoIV.setY(10);
        //this.getChildren().add(logoIV);
        
        
        //初始化dco
        dco=new DiCengOper(primaryStage, true, "三维地层操作",controller);
        //attachDco
        controller.attachDco(dco);
        dco.addController(controller);
        toolbox=new ImageView("Image/gongjuxiang_1.png");
        toolbox.xProperty().bind(primaryStage.widthProperty().subtract(405));
        toolbox.setY(20);
        this.getChildren().add(toolbox);
        
        setting=new ImageView("Image/shezhi_1.png");
        setting.xProperty().bind(primaryStage.widthProperty().subtract(330));
        setting.setY(20);
        this.getChildren().add(setting);
        min=new ImageView("Image/zuixiaohua_1.png");
        min.xProperty().bind(primaryStage.widthProperty().subtract(265));
        min.setY(20);
        this.getChildren().add(min);
        max=new ImageView("Image/zuidahua_1.png");
        max.xProperty().bind(primaryStage.widthProperty().subtract(185));
        max.setY(20);
        this.getChildren().add(max);
        exit=new ImageView("Image/likai_1.png");
        exit.xProperty().bind(primaryStage.widthProperty().subtract(100));
        exit.setY(18);
        this.getChildren().add(exit);
        toolbox.setOnMouseEntered(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                Image i=new Image("Image2/gongjuxiang.png");
                toolbox.setImage(i);
            }
        });
        toolbox.setOnMouseExited(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                Image i=new Image("Image/gongjuxiang_1.png");
                toolbox.setImage(i);
            }
        });
        setting.setOnMouseEntered(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                Image i=new Image("Image2/shezhi.png");
                setting.setImage(i);
            }
        });
        setting.setOnMouseExited(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                Image i=new Image("Image/shezhi_1.png");
                setting.setImage(i);
            }
        });
        min.setOnMouseEntered(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                Image i=new Image("Image2/zuixiaohua.png");
                min.setImage(i);
            }
        });
        min.setOnMouseExited(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                Image i=new Image("Image/zuixiaohua_1.png");
                min.setImage(i);
            }
        });
        max.setOnMouseEntered(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                Image i=new Image("Image2/zuidahua.png");
                max.setImage(i);
                
            }
        });
        max.setOnMouseExited(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                Image i=new Image("Image/zuidahua_1.png");
                max.setImage(i);
            }
        });
        exit.setOnMouseEntered(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                Image i=new Image("Image2/likai.png");
                exit.setImage(i);
            }
        });
        exit.setOnMouseExited(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                Image i=new Image("Image/likai_1.png");
                exit.setImage(i);
            }
        });
        min.setOnMouseClicked(new EventHandler<MouseEvent>() {

            public void handle(MouseEvent t) {
//                primaryStage.hide();
                primaryStage.setHeight(20);
                primaryStage.setWidth(20);
            }
        });
        //设置点击事件
        max.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                boolean loadWellB=true;
                threeDMain.getJFrame().setAlwaysOnTop(true);
                threeDMain.getJFrame().setVisible(true);
                threeDMain.getJFrame().setAlwaysOnTop(false);
                if(controller.getJmeCanvas().isVIEW_3D())
                    dco.setVisible(false);
                else
                    dco.setVisible(true);
                dco.setAlwaysOnTop(true);
                dco.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                if(HUIZHICOUNT==0)
                    loadWellB=controller.getTreeForJME().fillTree();
                if (loadWellB) {
                    HUIZHICOUNT++;
                }
                
                System.out.print("点击了3D");
            }
        });
        exit.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                try {
                    File file=new File("d:\\labdb.db");
                    if(file.exists()){
                        file.delete();
                    }
                    System.out.println("我正在删数据库");
                } catch (Exception e) {
                }finally{
                    
                    System.exit(0);
                }
                
            }
        });
        //设置目前用于恢复上一次数据
        setting.setOnMouseClicked(new EventHandler<MouseEvent>() {

            public void handle(MouseEvent t) {
                File f1=new File("d:/labdb.bak");
                File f2=new File("d:/labdb.db");
                File tmp=new File("d:/tmp.tmp");
                if (f1.isFile()&&f2.isFile()&&tmp.isFile()) {
                    if (SHUJUHUIFUI>0) {
                        if (MyFileChannel.fileChannelCopy(f2, tmp)) {
                            MyFileChannel.fileChannelCopy(tmp, f1);
                            System.out.print("已经将数据进行备份");
                        }
                    }else{
                        if (MyFileChannel.fileChannelCopy(f1, f2)) {
                            controller.getDco().setPage();
                            System.out.print("成功恢复上一次数据");
                        }
                    }
                    
                }else{
                    try {
                        f1.createNewFile();
                        tmp.createNewFile();
                        MyFileChannel.fileChannelCopy(f2, f1);
                    } catch (IOException ex) {
                        Logger.getLogger(Header.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                SHUJUHUIFUI++;
            }
        });
        //链接各种tooltip
        Tooltip.install(setting, setTip);
        Tooltip.install(logoIV, logoTip);
        Tooltip.install(toolbox, toolTip);
        Tooltip.install(min, minTip);
        Tooltip.install(max, maxTip);
        Tooltip.install(exit, exitTip);
        
    }
}
